package cn.eyesea.cc.util;

import cn.eyesea.cc.config.GlobalConfig;
import cn.eyesea.cc.entity.FieldInfo;
import cn.eyesea.cc.entity.DomainInfo;
import lombok.extern.slf4j.Slf4j;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0
 * @title: DBUtil
 * @description: ${DESCRIPTION}
 * @author: wanghuilong
 * @date: 2020-06-10 18:03
 * @copyright: http://fingo.com.my/
 */
@Slf4j
public class DBUtil {

    static GlobalConfig dbInfo;

    static Connection conn = null;
    static Statement stmt = null;
    static ResultSet rs = null;

    public static void setDbInfo(GlobalConfig dbInfo) {
        DBUtil.dbInfo = dbInfo;
    }

    private static void init() {
        try {
            if (null == dbInfo) {
                log.info("dbInfo is null");
                return;
            }

            // 注册 JDBC 驱动
            Class.forName(dbInfo.getJDBC_DRIVER());

            // 打开链接
            conn = DriverManager.getConnection(dbInfo.getDB_URL(), dbInfo.getUSER(), dbInfo.getPASS());
            // 执行查询
            stmt = conn.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void close() {
        try {
            // 完成后关闭
            if (null != rs) {
                rs.close();
            }
            if (null != stmt) {
                stmt.close();
            }
            conn.close();
        } catch (SQLException se) {
            // 处理 JDBC 错误
            se.printStackTrace();
        } catch (Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException se2) {
            }// 什么都不做
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException se) {
                se.printStackTrace();
            }
        }
    }

    public static List<DomainInfo> getTables() {
        List<DomainInfo> list = new ArrayList<>();
        init();
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT TABLE_NAME,TABLE_COMMENT FROM TABLES where TABLE_SCHEMA = '" + dbInfo.getACT_DATABASE() + "'");
            while (rs.next()) {
                DomainInfo tableInfo = new DomainInfo();
                tableInfo.setName(rs.getString("TABLE_COMMENT"));
                tableInfo.setTable(rs.getString("TABLE_NAME"));
                list.add(tableInfo);
            }
        } catch (SQLException throwables) {
            log.error("error:{}", throwables);
        }
        close();
        return list;
    }


    public static List<FieldInfo> getTableColumnInfo(String tableName) {
        List<FieldInfo> list = new ArrayList<>();
        init();
        try {
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT COLUMN_NAME, COLUMN_TYPE,COLUMN_COMMENT,IS_NULLABLE,COLUMN_KEY,EXTRA,COLUMN_DEFAULT FROM COLUMNS where TABLE_NAME = '" + tableName + "' and TABLE_SCHEMA = '" + dbInfo.getACT_DATABASE() + "'");
            while (rs.next()) {
                FieldInfo fieldInfo = new FieldInfo();
                fieldInfo.setField(rs.getString("COLUMN_NAME"));
                fieldInfo.setName(rs.getString("COLUMN_COMMENT"));
                fieldInfo.setDbType(rs.getString("COLUMN_TYPE"));
                fieldInfo.setAreNull("YES".equals(rs.getString("IS_NULLABLE")));
                fieldInfo.setKey(rs.getString("COLUMN_KEY"));
                fieldInfo.setDefaultValue(rs.getString("COLUMN_DEFAULT"));
                fieldInfo.setExtra(rs.getString("EXTRA"));
                list.add(fieldInfo);
            }
        } catch (SQLException throwables) {
            log.error("error:{}", throwables);
        }
        close();
        return list;
    }
}
